/*
 * USB工具函数头文件
 * 功能：定义USB主机、设备和Gadget操作相关的函数和结构体
 * 作者：test_code项目
 */

#ifndef USB_UTILS_H
#define USB_UTILS_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <dirent.h>
#include <stdint.h>

// USB相关常量定义
#define USB_MAX_PATH_LEN        512
#define USB_MAX_BUFFER_SIZE     65536
#define USB_DEFAULT_TIMEOUT     5000    // 5秒超时
#define USB_SERIAL_BUFFER_SIZE  4096
#define USB_MAX_DEVICES         64

// USB错误代码
#define USB_SUCCESS             0
#define USB_ERROR_INIT          -1
#define USB_ERROR_DEVICE_NOT_FOUND  -2
#define USB_ERROR_OPEN_FAILED   -3
#define USB_ERROR_READ_FAILED   -4
#define USB_ERROR_WRITE_FAILED  -5
#define USB_ERROR_TIMEOUT       -6
#define USB_ERROR_INVALID_PARAM -7
#define USB_ERROR_NO_MEMORY     -8

// USB设备类型枚举
typedef enum {
    USB_CLASS_PER_INTERFACE = 0x00,
    USB_CLASS_AUDIO = 0x01,
    USB_CLASS_COMM = 0x02,
    USB_CLASS_HID = 0x03,
    USB_CLASS_PHYSICAL = 0x05,
    USB_CLASS_IMAGE = 0x06,
    USB_CLASS_PRINTER = 0x07,
    USB_CLASS_MASS_STORAGE = 0x08,
    USB_CLASS_HUB = 0x09,
    USB_CLASS_DATA = 0x0A,
    USB_CLASS_SMART_CARD = 0x0B,
    USB_CLASS_CONTENT_SECURITY = 0x0D,
    USB_CLASS_VIDEO = 0x0E,
    USB_CLASS_PERSONAL_HEALTHCARE = 0x0F,
    USB_CLASS_DIAGNOSTIC_DEVICE = 0xDC,
    USB_CLASS_WIRELESS = 0xE0,
    USB_CLASS_APPLICATION = 0xFE,
    USB_CLASS_VENDOR_SPEC = 0xFF
} usb_device_class_t;

// USB速度枚举
typedef enum {
    USB_SPEED_UNKNOWN = 0,
    USB_SPEED_LOW = 1,      // 1.5 Mbps
    USB_SPEED_FULL = 2,     // 12 Mbps
    USB_SPEED_HIGH = 3,     // 480 Mbps
    USB_SPEED_SUPER = 4,    // 5 Gbps
    USB_SPEED_SUPER_PLUS = 5 // 10 Gbps
} usb_speed_t;

// USB设备信息结构体
typedef struct {
    uint16_t vendor_id;
    uint16_t product_id;
    uint16_t device_version;
    uint8_t device_class;
    uint8_t device_subclass;
    uint8_t device_protocol;
    uint8_t max_packet_size;
    usb_speed_t speed;
    char manufacturer[256];
    char product[256];
    char serial_number[256];
    char device_path[USB_MAX_PATH_LEN];
    int bus_number;
    int device_address;
} usb_device_info_t;

// USB设备句柄（抽象）
typedef struct usb_device_handle {
    void *handle;           // libusb设备句柄
    uint16_t vendor_id;
    uint16_t product_id;
    int interface_number;
    int endpoint_in;
    int endpoint_out;
    int timeout;
} usb_device_handle_t;

// USB串口设备句柄
typedef struct usb_serial_handle {
    int fd;                 // 文件描述符
    char device_path[USB_MAX_PATH_LEN];
    struct termios old_termios;
    int timeout;
} usb_serial_handle_t;

// USB测试统计信息
typedef struct {
    size_t bytes_sent;
    size_t bytes_received;
    size_t total_operations;
    size_t successful_operations;
    size_t failed_operations;
    double average_speed_mbps;
    double total_time_seconds;
} usb_test_stats_t;

// USB主机模式函数
int usb_host_init(void);
void usb_host_cleanup(void);
usb_device_handle_t *usb_host_open_device(uint16_t vendor_id, uint16_t product_id);
usb_device_handle_t *usb_host_open_device_by_path(const char *device_path);
void usb_host_close_device(usb_device_handle_t *device);
int usb_host_get_device_info(usb_device_handle_t *device, usb_device_info_t *info);

// USB主机读写操作
int usb_host_read(usb_device_handle_t *device, unsigned char *buffer, size_t length, int *transferred);
int usb_host_write(usb_device_handle_t *device, const unsigned char *buffer, size_t length, int *transferred);
int usb_host_bulk_transfer(usb_device_handle_t *device, int endpoint, unsigned char *buffer, size_t length, int *transferred, int timeout);

// USB主机测试函数
int usb_host_read_test(usb_device_handle_t *device, size_t data_size, int test_count);
int usb_host_write_test(usb_device_handle_t *device, size_t data_size, int test_count);
int usb_host_loopback_test(usb_device_handle_t *device, size_t data_size, int test_count);
int usb_host_speed_test(usb_device_handle_t *device, size_t data_size, int test_count);

// USB设备扫描函数
int usb_scan_all_devices(void);
void usb_scan_serial_devices(void);
void usb_scan_storage_devices(void);
void usb_scan_hid_devices(void);

// USB串口设备函数
usb_serial_handle_t *usb_serial_open(const char *device_path);
void usb_serial_close(usb_serial_handle_t *serial);
int usb_serial_configure(usb_serial_handle_t *serial, int baudrate, int databits, int stopbits, char parity);
int usb_serial_read(usb_serial_handle_t *serial, unsigned char *buffer, size_t length, int timeout);
int usb_serial_write(usb_serial_handle_t *serial, const unsigned char *buffer, size_t length);
int usb_serial_flush(usb_serial_handle_t *serial);

// USB串口测试函数
int usb_serial_read_test(usb_serial_handle_t *serial, size_t data_size, int test_count);
int usb_serial_write_test(usb_serial_handle_t *serial, size_t data_size, int test_count);
int usb_serial_loopback_test(usb_serial_handle_t *serial, size_t data_size, int test_count);
int usb_serial_speed_test(usb_serial_handle_t *serial, size_t data_size, int test_count);

// USB Gadget函数
int usb_gadget_init(void);
void usb_gadget_cleanup(void);
int usb_gadget_enable_function(const char *function_name);
int usb_gadget_disable_function(const char *function_name);
int usb_gadget_read_test(size_t data_size, int test_count);
int usb_gadget_write_test(size_t data_size, int test_count);
int usb_gadget_loopback_test(size_t data_size, int test_count);

// 系统信息和工具函数
void usb_show_system_info(void);
void usb_print_device_info(const usb_device_info_t *info);
void usb_print_test_stats(const usb_test_stats_t *stats);
const char *usb_speed_to_string(usb_speed_t speed);
const char *usb_class_to_string(uint8_t device_class);
const char *usb_error_string(int error_code);

// 数据生成和验证函数
void usb_generate_test_data(unsigned char *buffer, size_t length, int pattern);
int usb_verify_test_data(const unsigned char *buffer, size_t length, int pattern);
void usb_print_hex_data(const unsigned char *data, size_t length, size_t max_display);

// 时间和性能测量函数
double usb_get_time_seconds(void);
double usb_calculate_speed_mbps(size_t bytes, double time_seconds);
void usb_delay_ms(int milliseconds);

// 调试和日志函数
void usb_debug_print(const char *format, ...);
void usb_error_print(const char *format, ...);
void usb_info_print(const char *format, ...);

// USB规格常量
#define USB_ENDPOINT_IN         0x80
#define USB_ENDPOINT_OUT        0x00
#define USB_REQUEST_TYPE_STANDARD   0x00
#define USB_REQUEST_TYPE_CLASS      0x20
#define USB_REQUEST_TYPE_VENDOR     0x40

// 常用测试模式
#define USB_TEST_PATTERN_ZEROS      0
#define USB_TEST_PATTERN_ONES       1
#define USB_TEST_PATTERN_COUNTER    2
#define USB_TEST_PATTERN_RANDOM     3

#endif // USB_UTILS_H